var t = null,
  appid = null,
  cxjz = 0;
var qbbdsj = new Array(),
  bfbdsj = new Array(),
  bdsjl = null,
  zsgs = 20;
var t = ""; //搜索框的文字
var app = getApp();
Page({
  data: {
    qbbd: [
      ['0', '理综测试得分榜', '理综', '58']
    ],
    xpznr: null,
    TabCur: 0,
    scrollLeft: 0, //导航栏
    bdsj: null, //榜单数据
    bdname: null, //当前榜单
    bdnume: null, //当前榜单对应的测试num
    dqnume: null, //现在的榜单
    id: null,
    csnr: null,
    jzts: false,
    ssnr: null //搜索内容
  },
  onReady: function () {
    var o = this;
    if (app.globalData.appid == null) {
      wx.login({
        success: function (n) {
          var code1 = n.code;
          wx.request({
            url: app.globalData.url +"ndzscs/phb/hqopenid.php",
            method: "GET",
            header: {
              "content-type": "application/json"
            },
            data: {
              code: code1
            },
            success: function (s) {
              app.globalData.appid = s.data;
              appid = s.data;
              o.setData({
                id: appid
              })
            },
          }) //获取openid
        }
      })
    }//appid为空重新加载
    else {
      appid = app.globalData.appid;
      o.setData({
        id: appid
      })
    }//提前加载成功
  },
  onShow() {
    if (cxjz == 1) {
      var s = {
        'num': this.data.dqnume
      }
      this.onLoad(s);
      cxjz = 0;
    }
  },
  onLoad: function (s) {
    var o = this;
    bfbdsj = new Array();
    if (s.num != null) {
      o.setData({
        dqnume: Number(s.num),
      })
    } else {
      o.setData({
        dqnume: 0,
      })
    }
    var i = o.data.dqnume;
    wx.request({
      url: app.globalData.url +"ndzscs/phb/main.php",
      method: "GET",
      data: {},
      header: {
        "content-type": "application/json"
      },
      success: function (e) {
        o.setData({
          qbbd: e.data,
        })
        wx.request({
          url: app.globalData.url +"ndzscs/phb/hqphb.php",
          method: "GET",
          data: {
            tm: o.data.dqnume + 1
          },
          header: {
            "content-type": "application/json"
          },
          success: function (e) {
            qbbdsj = e.data; //保存全部榜单
            for (bdsjl = 0; bdsjl < zsgs && e.data[bdsjl] != null; bdsjl++) {
              bfbdsj[bdsjl] = e.data[bdsjl];
            }
            o.setData({
              bdsj: bfbdsj,
              bdname: o.data.qbbd[i][1],
              bdnume: o.data.qbbd[i][3],
              dqnume: Number(o.data.qbbd[i][0]),
              TabCur: o.data.dqnume,
              scrollLeft: (o.data.dqnume - 1) * 60,
            })
            if (e.data[bdsjl] == null) {
              o.setData({
                jzts: true
              })
            } else {
              o.setData({
                jzts: false
              })
            }
          }
        }); //获取对应的榜单
      }
    }); //获取全部榜单
  },
  onReachBottom: function () {
    let o = this;
    if (qbbdsj[bdsjl] == null) {
      o.setData({
        jzts: true
      })
    } else {
      for (var i = bdsjl; bdsjl < i + zsgs && qbbdsj[bdsjl] != null; bdsjl++) {
        bfbdsj[bdsjl] = qbbdsj[bdsjl];
      }
      o.setData({
        bdsj: bfbdsj
      })
      if (qbbdsj[bdsjl] == null) {
        o.setData({
          jzts: true
        })
      }
    }
  },
  onShareAppMessage: function () {
    wx.showShareMenu();
    var o = this;
    return {
      title: "快来通过测试看看你的得分能在“" + o.data.bdname + "”中排第几名吧！",
      path: "/pages/home/home?num=" + o.data.dqnume
    }
  },
  onPullDownRefresh: function () {
    var o = this;
    var s = {
      'num': o.data.dqnume
    };
    o.onLoad(s);
    wx.stopPullDownRefresh();
  },
  tabSelect(t) {
    bfbdsj = new Array();
    var o = this;
    o.setData({
      TabCur: t.currentTarget.dataset.id,
      scrollLeft: (t.currentTarget.dataset.id - 1) * 60,
    })
    wx.request({
      url: app.globalData.url +"ndzscs/phb/hqphb.php",
      method: "GET",
      data: {
        tm: t.currentTarget.dataset.id + 1
      },
      header: {
        "content-type": "application/json"
      },
      success: function (e) {
        qbbdsj = e.data; //保存全部榜单
        for (bdsjl = 0; bdsjl < zsgs && e.data[bdsjl] != null; bdsjl++) {
          bfbdsj[bdsjl] = e.data[bdsjl];
        }
        o.setData({
          bdsj: bfbdsj,
          bdname: o.data.qbbd[t.currentTarget.dataset.id][1],
          bdnume: o.data.qbbd[t.currentTarget.dataset.id][3],
          dqnume: Number(o.data.qbbd[t.currentTarget.dataset.id][0]),
        })
        if (e.data[bdsjl] == null) {
          o.setData({
            jzts: true
          })
        } else {
          o.setData({
            jzts: false
          })
        }
      },
      fail: function () {
        wx.showToast({
          title: "数据获取失败，请下拉刷新！",
          icon: "none",
          duration: 800
        });
      }
    });
  }, //导航栏
  ksdb: function () {
    var s = this;
    var o = s.data.bdnume;
    wx.request({
      url: app.globalData.url +"ndzscs/phb/dbss.php",
      method: "GET",
      data: {
        tm: o,
      },
      header: {
        "content-type": "application/json"
      },
      success: function (e) {
        s.setData({
          csnr: e.data,
        })
        wx.navigateTo({
          url: "/pages/ceshi/ceshi?tm=" + s.data.csnr[0][1] + "&tp=" + s.data.csnr[0][4] + "&bjtp=" + s.data.csnr[0][5] + "&xxjs=" + s.data.csnr[0][3] + "&num=" + s.data.csnr[0][0] + "&xpzcs=0" + "&zdy=" + s.data.csnr[0][10] + "&csrs=" + s.data.csnr[0][6],
        });
        cxjz = 1;
      },
    });
  },
  scwdsj: function () {
    var s = this;
    qq.showModal({
      title: '提示',
      content: '确定要将你的得分从该榜单中删除吗？',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.url +"ndzscs/phb/scwdsj.php",
            method: "GET",
            data: {
              tm: s.data.dqnume,
              id: s.data.id
            },
            header: {
              "content-type": "application/json"
            },
            success: function () {
              var o = {
                'num': s.data.dqnume
              };
              s.onLoad(o);
            },
          });
        } else if (res.cancel) { }
      }
    })
  },
  phbsm: function () {
    qq.showModal({
      title: 'Top榜说明',
      showCancel: false,
      content: '1.当您完成对应测试后，您的得分将会加入Top榜，您可随时删除自己的得分数据。 2.当您的的头像或昵称存在违规时，您的得分数据将会被删除！',
    })
  },
  getInput: function (o) {
    t = o.detail.value;
  },
  cztm: function () {
    var o = this;
    if (t != '') {
      for (var i = 0; o.data.qbbd[i] != null; i++) {
        if (o.data.qbbd[i][2].indexOf(t) >= 0) {
          var s = {
            'num': i
          };
          o.onLoad(s);
          return;
        }
      }
      wx.showToast({
        title: "没有查找到对应的排行榜！",
        icon: "none",
        duration: 800
      });
    } else {
      wx.showToast({
        title: "请输入要查找的内容！",
        icon: "none",
        duration: 800
      });
    }
  },
})